iT邦幫忙

2025 iThome 鐵人賽

DAY 10
0
生成式 AI

Multi-Agent 實戰:開發多代理智慧小幫手系列 第 10

【Day 10】 Master Agent 基礎實作(上)

  • 分享至 

  • xImage
  •  

前兩篇我們介紹了 Google ADK 和 Master Agent,
這篇就要實際操作一次,來用 Google ADK 建立 root_agent,也就是 Master Agent。

事前準備

取得 API Key

  1. 打開 Google AI Studio,點擊 Get Started 註冊並登入。
    https://ithelp.ithome.com.tw/upload/images/20250924/20168456tApHu8nRUy.png
  2. 左下角有個 Get API Key,點擊它。
    https://ithelp.ithome.com.tw/upload/images/20250924/20168456lOUHy0onvX.png
  3. 在右邊點 Create API Key
    https://ithelp.ithome.com.tw/upload/images/20250924/20168456zHiD1BzVww.png
  4. API Key 名稱可以自己取,設定完後點 Create Project
    https://ithelp.ithome.com.tw/upload/images/20250924/20168456VWk3bDKWYB.png
  5. project 名稱可以自己取,設定完後點 Create Project
    https://ithelp.ithome.com.tw/upload/images/20250924/20168456F74FBGWgUp.png
  6. 建立好 Project 後,回到畫面選擇剛剛建立的 Project。
    https://ithelp.ithome.com.tw/upload/images/20250924/20168456lPdvOJot6Y.png
  7. API Key 建立完成後,點擊即可複製。
    https://ithelp.ithome.com.tw/upload/images/20250924/201684569fn9XYjkU7.png

Python

在 VSCode 的終端機輸入:

pip install google-adk

如果你使用虛擬環境,請先啟動虛擬環境再安裝套件。

實作

我們會在同一個資料夾下建立三個檔案:__init__.py.envroot_agent.py,並貼上以下的程式碼。

.env

GOOGLE_GENAI_USE_VERTEXAI=FALSE
GOOGLE_API_KEY=YOUR_GOOGLE_API_KEY

__init__.py

from .RootAgent import root_agent

root_agent.py

from google.adk.agents import Agent

def add_numbers(a: int, b: int) -> dict:
    """Adds two numbers and returns the result.
    Args:
        a (int): First number
        b (int): Second number
    Returns:
        dict: status and result
    """
    try:
        result = a + b
        return {"status": "success", "result": f"The sum of {a} and {b} is {result}."}
    except Exception as e:
        return {"status": "error", "error_message": str(e)}

def reverse_text(text: str) -> dict:
    """Reverses the given text string.
    Args:
        text (str): Input string to reverse
    Returns:
        dict: status and result
    """
    try:
        reversed_text = text[::-1]
        return {"status": "success", "result": f"The reversed text is: {reversed_text}"}
    except Exception as e:
        return {"status": "error", "error_message": str(e)}

root_agent = Agent(
    name="math_text_agent",
    model="gemini-2.0-flash",
    description="Agent to perform simple math operations and text manipulations.",
    instruction="You are a helpful agent who can solve simple math problems and reverse text.",
    tools=[add_numbers, reverse_text],
)

這樣我們的 Agent 就算大致完成了!
下一篇文章,我們會帶大家深入解析程式碼,並示範如何測試這個 Agent 的功能。


上一篇
【Day 09】認識 Master Agent
下一篇
【Day 11】 Master Agent 基礎實作(下)
系列文
Multi-Agent 實戰:開發多代理智慧小幫手11
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言